package com.itbuka.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.github.pagehelper.Page;
import com.itbuka.order.domain.Task;
import com.itbuka.order.mapper.TaskMapper;
import com.itbuka.order.service.TaskService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import java.util.List;


/**
* @author ld
* @description 针对表【tb_task】的数据库操作Service实现
* @createDate 2024-08-10 19:40:45
*/
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public List<Task> selectAll() {
        LambdaQueryWrapper <Task> lqw = new LambdaQueryWrapper<>();
        return taskMapper.selectList(lqw);
    }

    @Override
    public List<Task> selectList(Task task) {
        LambdaQueryWrapper <Task> lqw = this.lqw(task);
        return taskMapper.selectList(lqw);
    }

    @Override
    public Integer insert(Task task) {
        return taskMapper.insert(task);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                Task task = new Task();
                task.setId(Long.parseLong(id));
                taskMapper.updateById(task);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(Task task) {
        return taskMapper.updateById(task);
    }

    @Override
    public Page<Task> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Task>)this.selectAll();
    }

    @Override
    public Page<Task> pageList(Task task, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Task>)this.selectList(task);
    }

    public LambdaQueryWrapper<Task> lqw(Task task){
        LambdaQueryWrapper <Task> lqw = new LambdaQueryWrapper<>();
        if (task.getId() != null){
            lqw.eq(Task::getId,task.getId());
        }
        if (task.getCreateTime() != null){
            lqw.eq(Task::getCreateTime,task.getCreateTime());
        }
        if (task.getUpdateTime() != null){
            lqw.eq(Task::getUpdateTime,task.getUpdateTime());
        }
        if (task.getDeleteTime() != null){
            lqw.eq(Task::getDeleteTime,task.getDeleteTime());
        }
        if (task.getTaskType() != null){
            lqw.eq(Task::getTaskType,task.getTaskType());
        }
        if (task.getMqExchange() != null){
            lqw.eq(Task::getMqExchange,task.getMqExchange());
        }
        if (task.getMqRoutingkey() != null){
            lqw.eq(Task::getMqRoutingkey,task.getMqRoutingkey());
        }
        if (task.getRequestBody() != null){
            lqw.eq(Task::getRequestBody,task.getRequestBody());
        }
        if (task.getStatus() != null){
            lqw.eq(Task::getStatus,task.getStatus());
        }
        if (task.getErrormsg() != null){
            lqw.eq(Task::getErrormsg,task.getErrormsg());
        }
        return lqw;
    }
}




